#include "angle.h"
#include "../bulk_storage/bulk_storage.h"

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int main()
{
  char line[83];
  struct angle * ang;
  struct store * angstore = store_open_cached("ang_store", "ang_store.fat",
					     sizeof(struct angle), (off_t)100, (off_t)2000);
  int i;
  
  for (i = 0; i < 5; i++)
      fgets(line, 82, stdin); //In *.ang file, the data just begin from line 10; 
  
  while((fgets(line, 82, stdin) !=NULL) && (strncmp("SCRIPT>>>> nolog", line, 16))) {
    
    i = store_new (angstore);
    
    ang = store_read(angstore, i);
    
    strncpy(ang->aaname, &(line[5]), 3);
    
    line[15] ='\0';
    ang->aanumber = atoi(&(line[11]));
    
    ang->aachain = line[18];
    
    line[28] = '\0';
    ang->phi = atof(&(line[22]));
    
    line[35] = '\0';
    ang->psi = atof(&(line[29]));
    
    if(line[42]=='\0'){
      ang->omega = atof(&(line[36]));
      ang->chi1 = 0.0;
      ang->chi2 = 0.0;
      ang->chi3 = 0.0;
      ang->chi4 = 0.0;
      ang->chi5 = 0.0;
    }
    else {
      line[42] = '\0';
      ang->omega = atof(&(line[36]));
      
      if(line[49]=='\0'){
	ang->chi1 = atof(&(line[43]));
	ang->chi2 = 0.0;
	ang->chi3 = 0.0;
	ang->chi4 = 0.0;
	ang->chi5 = 0.0;
      }
      else {
	line[49] = '\0';
	ang->chi1 = atof(&(line[43]));
	
	if(line[56] == '\0') {
	  ang->chi2 = atof(&(line[50]));
	  ang->chi3 = 0.0;
	  ang->chi4 = 0.0;
	  ang->chi5 = 0.0;
	}
	else {
	  line[56] = '\0';
	  ang->chi2 = atof(&(line[50]));
	  
	  if(line[63] == '\0') {
	    ang->chi3 = atof(&(line[57]));
	    ang->chi4 = 0.0;
	    ang->chi5 = 0.0;
	  }
	  else {
	    line[63] = '\0';
	    ang->chi3 = atof(&(line[57]));
	    
	    if(line[70] == '\0') {
	      ang->chi4 = atof(&(line[64]));
	      ang->chi5 = 0.0;
	    }
	    else {
	      line[70] = '\0';
	      ang->chi4 = atof(&(line[64]));
	      
	      line[77] = '\0';
	      ang->chi5 = atof(&(line[71]));
	    }
	  }
	}
      }
    }

    store_write(angstore, i);
    store_release(angstore, i);
    //printf("name= %c%c%c, number= %d, chain= %c, phi= %f, psi= %f, omega= %f, chi1=%f, chi2=%f, chi3=%f, chi4=%f, chi5=%f\n", ang->aaname[0], ang->aaname[1],ang->aaname[2], ang->aanumber, ang->aachain,  ang->phi, ang->psi, ang->omega, ang->chi1, ang->chi2, ang->chi3, ang->chi4, ang->chi5);
  }
  
  store_close(angstore);
  return 0;
}
    
      
	      

    
    
    
